<?php

namespace app\model\admin;

use app\model\BaseModel;

/**
 * 管理员操作日志模型
 * 对应数据表：oa_admin_log
 */
class AdminLog extends BaseModel
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = 'admin_log';

    /**
     * 主键名
     * @var string
     */
    protected $pk = 'id';

    /**
     * 字段类型转换
     * @var array
     */
    protected $type = [
        'id' => 'integer',
        'admin_id' => 'integer',
        'username' => 'string',
        'url' => 'string',
        'title' => 'string',
        'content' => 'string',
        'ip' => 'string',
        'useragent' => 'string',
        'create_time' => 'integer',
    ];

    /**
     * 可搜索字段
     * @var array
     */
    protected $searchField = ['username', 'title', 'url', 'ip'];

    /**
     * 关联管理员
     * @return \think\model\relation\BelongsTo
     */
    public function admin()
    {
        return $this->belongsTo('app\model\admin\User', 'admin_id', 'id');
    }

    /**
     * 获取日志列表
     * @param array $params 查询参数
     * @return \think\Paginator
     */
    public function getLogList($params = [])
    {
        $where = [];
        
        if (!empty($params['admin_id'])) {
            $where[] = ['admin_id', '=', $params['admin_id']];
        }
        
        if (!empty($params['username'])) {
            $where[] = ['username', 'like', '%' . $params['username'] . '%'];
        }
        
        if (!empty($params['start_time'])) {
            $where[] = ['create_time', '>=', strtotime($params['start_time'])];
        }
        
        if (!empty($params['end_time'])) {
            $where[] = ['create_time', '<=', strtotime($params['end_time'])];
        }

        return $this->where($where)
            ->order('id', 'desc')
            ->paginate([
                'list_rows' => $params['limit'] ?? 15,
                'page' => $params['page'] ?? 1,
            ]);
    }

    /**
     * 记录管理员操作日志
     * @param int $adminId 管理员ID
     * @param string $username 用户名
     * @param string $url 操作URL
     * @param string $title 操作标题
     * @param string $content 操作内容
     * @param string $ip IP地址
     * @param string $userAgent 用户代理
     * @return bool
     */
    public function recordLog($adminId, $username, $url, $title, $content = '', $ip = '', $userAgent = '')
    {
        return $this->save([
            'admin_id' => $adminId,
            'username' => $username,
            'url' => $url,
            'title' => $title,
            'content' => $content,
            'ip' => $ip,
            'useragent' => $userAgent,
            'create_time' => time(),
        ]);
    }

    /**
     * 搜索器：按用户名搜索
     * @param \think\db\Query $query
     * @param string $value
     */
    public function searchUsernameAttr($query, $value)
    {
        $query->where('username', 'like', '%' . $value . '%');
    }

    /**
     * 搜索器：按IP地址搜索
     * @param \think\db\Query $query
     * @param string $value
     */
    public function searchIpAttr($query, $value)
    {
        $query->where('ip', 'like', '%' . $value . '%');
    }

    /**
     * 搜索器：按时间范围搜索
     * @param \think\db\Query $query
     * @param array $value
     */
    public function searchCreateTimeAttr($query, $value)
    {
        if (is_array($value) && count($value) === 2) {
            $query->whereBetween('create_time', [strtotime($value[0]), strtotime($value[1])]);
        }
    }
}